package com.ddxcloud.common.utils;

import org.apache.commons.lang3.StringUtils;

public class DesensUtils {
    /**
     * 姓名脱敏
     * @param realName
     * @return
     */
    public static String desensName(String realName) {
        // 去除真实姓名敏感信息
        if (StringUtils.isNotBlank(realName)) {
            if (realName.contains("*")){
                return realName;
            }
            if (realName.length() > 1) {
                if(realName.length() == 2){
                    realName =  realName.replaceFirst(realName.substring(0,1),"*");
                }
                if (realName.length() > 2) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < realName.length() - 2; i++){
                        sb.append("*");
                    }
                    realName = realName.replaceFirst(realName.substring(1,realName.length()-1) ,sb.toString());
                }
            }
        }
        return realName;
    }

    /**
     * 手机号脱敏
     * @param mobile
     * @return
     */
    public static String desensMobile(String mobile) {
        // 去除手机号敏感信息
        if (StringUtils.isNotBlank(mobile) && mobile.length() == 11) {
            if (mobile.contains("*")){
                return mobile;
            }
            mobile = mobile.replaceFirst(mobile.substring(3,7) ,"****");
        }
        return mobile;
    }

    /**
     * 银行卡脱敏
     * @param bankCard
     * @return
     */
    public static String desensBankCard(String bankCard) {
        // 去除银行卡敏感信息
        if (StringUtils.isNotBlank(bankCard)) {
            //防止替换报错
            if (bankCard.contains("*")){
                return bankCard;
            }
            bankCard =  bankCard.replaceFirst(bankCard.substring(3,bankCard.length() -3) ,"**********");
        }
        return bankCard;
    }

    /**
     * 身份证脱敏 保留前一位、后一位
     * @param idCard
     * @return
     */
    public static String desensIdCard(String idCard) {
        // 去除身份证敏感信息
        if (StringUtils.isNotBlank(idCard)) {
            //防止替换报错
            if (idCard.contains("*")){
                return idCard;
            }
            if (idCard.length() == 18){
                idCard =  idCard.replaceFirst(idCard.substring(1,idCard.length() - 1) ,"****************");
                return idCard;
            }
            if (idCard.length() == 15){
                idCard =  idCard.replaceFirst(idCard.substring(1,idCard.length() - 1) ,"*************");
            }
        }
        return idCard;
    }

    /**
     * 身份证脱敏 保留前一位、后四位
     * @param idCard
     * @return
     */
    public static String desensIdCardFour(String idCard) {
        // 去除身份证敏感信息
        if (StringUtils.isNotBlank(idCard)) {
            //防止替换报错
            if (idCard.contains("*")){
                return idCard;
            }
            if (idCard.length() == 18){
                idCard =  idCard.replaceFirst(idCard.substring(1,idCard.length() - 4) ,"****************");
                return idCard;
            }
            if (idCard.length() == 15){
                idCard =  idCard.replaceFirst(idCard.substring(1,idCard.length() - 4) ,"*************");
            }
        }
        return idCard;
    }

    /**
     * 港澳台证保留后四位不脱敏
     * @param idCard
     * @return
     */
    public static String replaceIdCard(String idCard) {
        // 去除身份证敏感信息
        if (StringUtils.isNotBlank(idCard) && idCard.length() > 4) {
            //防止替换报错
            if (idCard.contains("*")){
                return idCard;
            }
            StringBuilder sb = new StringBuilder();
            char[] chars = idCard.toCharArray();
            int len = chars.length;
            for (int i = 0; i < len; i++){
                if (i < len - 4){
                    sb.append("*");
                } else {
                    sb.append(chars[i]);
                }
            }
            return sb.toString();
        }
        return idCard;
    }

    /**
     * 固定电话脱敏
     * @param tel
     * @return
     */
    public static String desensTel(String tel) {
        if (StringUtils.isNotBlank(tel) && tel.length() >= 3) {
            //防止替换报错
            if (tel.contains("*")){
                return tel;
            }
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < tel.length() - 2; i++) {
                sb.append("*");
            }
            tel =  tel.replaceFirst(tel.substring(1,tel.length() - 1) ,sb.toString());
        }
        return tel;
    }

    public static void main(String[] args) {
        String msg = "qpp**";
        if (msg.contains("*")){
            System.out.println("111:" + "包含");
        }
        System.out.println(desensName("诸葛青云"));
        System.out.println(desensMobile("15261526693"));
        System.out.println(desensBankCard("6218381234564568"));
        System.out.println(desensIdCardFour("432224199204214110"));
        System.out.println(desensTel("010-8888"));
        System.out.println(replaceIdCard("H1234567890"));
    }

}
